﻿using System;
using System.Collections.Generic;
using Microsoft.PlayerFramework;
using Mutzl.Samples.VideoApp.Common;
using Mutzl.Samples.VideoApp.RssContent.Model;
using Mutzl.Samples.VideoApp.RssContent.ViewModel;
using Windows.System.Display;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Mutzl.Samples.VideoApp.RssContent.View
{
    public sealed partial class DetailView : LayoutAwarePage
    {
        public DetailView()
        {
            InitializeComponent();


            // Fix to enable player buttons in AppBar
            //http://playerframework.codeplex.com/wikipage?title=Applying%20Themes
            UpdateViewModel(videoPlayer.InteractiveViewModel);

        }

        private DetailViewModel ViewModel { get { return DataContext as DetailViewModel; } }


        private void UpdateViewModel(IInteractiveViewModel vm)
        {
            ReplayButton.ViewModel = vm;
            FullScreenButton.ViewModel = vm;
            MuteButton.ViewModel = vm;
        }


        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {

            var video = navigationParameter as Video;
            if (video == null) return;
            DataContext = new DetailViewModel(video);

            ViewModel.PropertyChanged += (sender, args) =>
                                             {
                                                 if (args.PropertyName == "IsFullScreen")
                                                 {
                                                     SetVideoPlayerMode();
                                                 }
                                             };

            if (ViewModel.IsSmoothStreaming)
            {
                var plugin = new Microsoft.PlayerFramework.Adaptive.AdaptivePlugin();
                videoPlayer.Plugins.Add(plugin);
            }

        }

        private DisplayRequest displayRequest = null;

        protected override void OnNavigatedTo(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {

            // damit der Screensaver nicht zuschlagen kann, wenn der User gerade ein Video betrachtet.
            if (displayRequest == null) displayRequest = new DisplayRequest();
            displayRequest.RequestActive();

            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            if (displayRequest != null)
            {
                displayRequest.RequestRelease();
                displayRequest = null;
            }

            base.OnNavigatedFrom(e);
        }

        private bool IsFullScreenAllowed
        {
            get
            {
                return (ApplicationView.Value == ApplicationViewState.FullScreenLandscape
                        || ApplicationView.Value == ApplicationViewState.Filled);
            }
        }


        private void SetVideoPlayerMode()
        {
            VisualStateManager.GoToState(this, ViewModel.IsFullScreen && IsFullScreenAllowed ? "Fullscreen" : "Normal", false);

            TopAppBar.IsOpen = false;
            BottomAppBar.IsOpen = false;
        }


        protected override string DetermineVisualState(ApplicationViewState viewState)
        {
            FullScreenButton.Visibility = IsFullScreenAllowed ? Visibility.Visible : Visibility.Collapsed;
            //if (!IsFullScreenAllowed) ViewModel.IsFullScreen = false;

            SetVideoPlayerMode();


            return base.DetermineVisualState(viewState);
        }

        private void VideoPlayer_OnMediaEnded(object sender, MediaPlayerActionEventArgs e)
        {
            ViewModel.IsFullScreen = false;
        }


    }
}
